Sorts the values along each column of target when column is true, top to bottom, or along each row when column is false, left to right. ascending picks smallest first (true) or biggest first (false). EACH column or row is sorted on its own, so the cells of a row do not stay together: a name and its score can end up apart (grid_sort_rows is the one that keeps rows whole). The cells must hold numbers or text.
grid_sort(target, column, ascending)| Name | Type |
|---|---|
target | grid |
column | bool |
ascending | bool |
Nothing.
Sorts the numbers or the text in target, a grid, along each column or along each row, depending on column. To put whole rows in order by one column, so that a row stays together, use grid_sort_rows instead.
Here is how column and ascending work together (for text, "smallest" means the A's first and "biggest" means the Z's first):
column | ascending | What it does |
|---|---|---|
true | true | goes through each column and puts its numbers in order, smallest at the top |
true | false | goes through each column and puts its numbers in order, biggest at the top |
false | true | goes through each row and puts its numbers in order, smallest on the left |
false | false | goes through each row and puts its numbers in order, biggest on the left |
So the first true or false picks the direction the numbers are sorted along, and the second picks smallest-first or biggest-first.
grid_sort(board, true, true); // every column, smallest at the top
grid_sort(board, false, false); // every row, biggest on the leftRead this part before you use it. Every column (or row) is sorted by itself, and none of them is told what the others did. So the cells of a row do not stay together when you sort the columns. A number can end up in a different row from the numbers that used to sit beside it.
Here is a grid 3 across and 3 down, before and after grid_sort(board, true, true):
Before After
3 9 1 1 2 1
1 2 8 2 5 4
2 5 4 3 9 8The first column was 3, 1, 2 and is now 1, 2, 3. The second was 9, 2, 5 and is now 2, 5, 9. The third was 1, 8, 4 and is now 1, 4, 8. Look at the top row: it was 3, 9, 1 and is now 1, 2, 1, which is not the same set of numbers at all, because each column was put in order by itself.
That matters if a row means something as a whole. Say a high-score table keeps a player's number in the first column and their score in the second:
Before After grid_sort(table, true, false)
player score player score
1 50 3 90
2 90 2 70
3 70 1 50grid_sort(table, true, false) sorts the player column and the score column separately, biggest first. The players come out 3, 2, 1 and the scores 90, 70, 50, so the table now says player 3 scored 90, but player 3 scored 70. The names were separated from their scores. If you need each row to stay together, grid_sort is the wrong tool: use grid_sort_rows, which puts the rows in order by the score column and keeps every player beside their own score.
grid_sort is the right tool when each column (or row) really is its own list of numbers: a row of heights to put in order, say, or a column of timers.
A grid of whole numbers or decimals is put in number order, and a grid of text is put in alphabetical order. A grid of yes-or-no values, of structs or of references is refused when the project is checked, before the game runs: GBML does not know what order those should go in, so it will not guess.
Text is put in order one character at a time, by each character's code. The code is the number a character has in the table of letters ("A" is 65, "a" is 97, "0" is 48, a space is 32: see string_code_at). That gives these rules:
| Rule | Example |
|---|---|
| a space comes before a digit, a digit before a capital letter, a capital letter before a small letter | " x", "10", "Zed", "ada" come in that order |
| every capital letter comes before every small letter. A capital and a small letter are not treated as the same letter | "Zed" comes before "ada", and "Apple" before "apple" |
| digits are compared one at a time | "10" comes before "9", because 1 comes before 9 |
| a text that is the start of a longer one comes first | "ab" comes before "abc" |
| a character past the plain keyboard ones (a code above 127) comes after all of them | it comes last |
If you want names to come out the way a dictionary would put them, write them all with a capital first letter, or all small. With ascending set to false the whole order is turned around.
To find a column, GBML needs to know how many cells are in a row. A grid only knows its width and height where it was made (grid_create(8, 6, 0), /// @capacity 8x6, or grid<int, 8, 6>). A grid with no size written like that, or a script's grid parameter, has none to give, and the call is refused, the same as grid_width is.
| You do this | What happens |
|---|---|
leave out column or ascending, or give a number instead of true or false | refused when checked. It needs all three arguments |
| sort a grid that has equal numbers, or equal pieces of text, in a column | they end up next to each other. Two equal values are the same value, so it does not matter which is which |
| give it a list or an array | refused when checked, and the message names the function that sorts that kind |
| use it where a value is needed | refused when checked. It gives nothing back |
Nothing is set aside: each column or row is sorted where it stands, never copied out. The time grows with the number of cells, and a little faster than that for a very long column or row. Comparing two pieces of text takes longer than comparing two numbers. No timing was measured.
var board = grid_create(3, 3, 0);
board[# 0, 0] = 3;
board[# 1, 0] = 9;
board[# 2, 0] = 1;
grid_sort(board, true, true); // each column, smallest at the top
grid_sort(board, false, false); // each row, biggest on the left
var words = grid_create(2, 2, "");
words[# 0, 0] = "pear";
words[# 1, 0] = "Zed";
words[# 0, 1] = "ada";
words[# 1, 1] = "Apple";
grid_sort(words, false, true); // each row, A first: "Zed" and "pear" in the top row, "Apple" and "ada" belowOne shell sort per column (a run of height places, the width apart) or per row (a run of width places in a row), each in place and each independent of the others. A column is sorted where it stands, never copied out, so a tall grid costs no stack. It needs the grid's width and height, which the checker reads from the declaration (`grid_create(8, 6, 0)`, `/// @capacity 8x6` or `grid<int, 8, 6>`) and writes into the C++ as plain numbers; a grid with none, such as a script's parameter, is refused, the same as grid_width. A shell sort done in place: it uses no extra memory, makes no copy, and never leaves the places that hold items. It is not stable, which cannot show for numbers or text (two equal values are the same value). The time grows a little faster than the number of items - about n times the square root of n in the worst case, usually much less - and comparing two texts takes longer than comparing two numbers (as many steps as the two texts have in common at the start). No timing was measured. Whole numbers, decimals and text build. Text is put in order one character at a time, by each character's code. A space comes before a digit, a digit before a capital letter and a capital letter before a small one, so "Zed" comes before "ada" (a capital and a small letter are NOT treated as the same letter), and a text that is the start of a longer one comes first ("ab" before "abc"). A character with a code above 127 comes after all the plain keyboard ones. There is no order for yes-or-no values, structs or references that GBML knows, so those are refused when the project is checked.